<?php

namespace App\Admin\Controllers;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use App\Models\Product;
use zgldh\QiniuStorage\QiniuStorage;
use Encore\Admin\Controllers\AdminController;

class ProductController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '商品管理';
    protected  $states = [
        'on'  => ['value' => 1, 'text' => '上架', 'color' => 'success'],
        'off' => ['value' => 0, 'text' => '下架', 'color' => 'danger'],
    ];

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Product());
        $grid->model()->orderBy('id', 'desc');
        $grid->column('id', __('Id'));
        $grid->column('image', __('图片'))->image('', 150, 150);
        $grid->column('name', __('名称'));
        $grid->column('style_no', __('型号'));
        $grid->column('size', __('尺寸'));
        $grid->column('material', __('材料'));
        $grid->column('price', __('价格'));
        $grid->column('is_up', __('是否上架'))->switch($this->states);
        $grid->column('created_at', __('创建时间'));
        $grid->column('updated_at', __('更新时间'));

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Product::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('image', __('图片'))->image();
        $show->field('name', __('名称'));
        $show->field('size', __('尺寸'));
        $show->field('style_no', __('型号'));
        $show->field('material', __('材料'));
        $show->field('price', __('价格'));
        $show->field('is_up', __('是否上架'));
        $show->field('created_at', __('创建时间'));
        $show->field('updated_at', __('更新时间'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Product());

        // $form->image('image', __('图片'))->uniqueName();
        $form->multipleImage('image', __('图片'))->uniqueName()->removable();
        $form->text('name', __('名称'));
        $form->text('style_no', __('型号'));
        $form->text('size', __('尺寸'));
        $form->text('material', __('材料'));
        $form->decimal('price', __('价格'));
        $form->switch('is_up', __('是否上架'))->states($this->states);
        return $form;
    }
}
